import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Random;


public class Client {
	
	private DataOutputStream out;
	private BufferedReader in;
	private Socket server;
	private int[] serverIPs = {5000, 5001, 5002, 5003, 5004};
	private int timeout = 500;
	
	public void getSocket() {
		int serverIP = 0;
		do {
			try {
				// Randomly pick a server
				Random generator = new Random();
				serverIP = serverIPs[generator.nextInt(serverIPs.length)];		
				//System.out.println("Attempting to connect to " + serverIP);
				SocketAddress sockaddr = new InetSocketAddress("localhost", serverIP);
				server = new Socket();			
				server.connect(sockaddr, timeout);
			} catch (UnknownHostException e) {
				//e.printStackTrace();
			} catch (SocketTimeoutException e) {
				//System.out.println("Connection timed out");
			} catch (IOException e) {
				//e.printStackTrace();
			}	
		} while(!server.isConnected());
		
				
		try {
			System.out.println("connect to " + serverIP);
			out = new DataOutputStream(server.getOutputStream());
			in = new BufferedReader(new InputStreamReader(server.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("EXCEPTION 2");
			e.printStackTrace();
		}
		
	}
	
	public String issueCommand(String command) {
		String response = "";
	
		try {
			out.writeBytes(command);
			response = in.readLine();			
		} catch (IOException e) {	
			System.out.println("EXCEPTION 3");
			e.printStackTrace();
		}

		return response;
	}
	
	public void closeConnection() {
		try {
			server.close();
		} catch (IOException e) {
			System.out.println("EXCEPTION 1");
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		String command = "";
		Client client = new Client();
		String output = "";
		
		while(true){
			try {
				// get command
				command = input.readLine();
				
				// exit if done
				if(command.equalsIgnoreCase("done")){
					break;
				}
				
				do {
					System.out.println("getting socket");
					// get socket
					client.getSocket();
					
					// process command
					output = client.issueCommand(command+ '\n');
				
					System.out.println("output returned " + output);
				} while(output.trim().equalsIgnoreCase("timeout") || output.trim().length() == 0);
				
				
				System.out.println("output = " + output);
				
				// close socket
				client.closeConnection();
				
				// output execution time
				//System.out.println("execution time: " + diff);
			} catch (IOException e1) {
				System.out.println("EXCEPTION");
				//e1.printStackTrace();					
			}				
		}
	}

}
